# import logging
import time
from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.Common.business_common_tool import BusinessCommonTools
from Case.python_ui.PageLocate.admin_bill_adjustment_locator import AdminBillAdjustmentLocator
from Case.python_ui.PageLocate.admin_ticket_management_locator import AdminTicketManagementLocator
import os
import xlrd
import openpyxl
import math
import nums_from_string
import datetime

class AdminBillAdjustmentPage(Basepage):

    # 进入adjustment菜单页面
    def enter_adjustment_page(self):
        time.sleep(2)
        doc = "进入adjustment菜单"
        for i in range(3):
            try:
                self.wait_elevisble(AdminTicketManagementLocator.dashboard_tab, doc)
                self.click_element(AdminTicketManagementLocator.dashboard_tab, doc)
                time.sleep(1)
                self.wait_elevisble(AdminBillAdjustmentLocator.adjustment_tab,doc)
                self.click_element(AdminBillAdjustmentLocator.adjustment_tab,doc)
                break
            except:
                self.driver.refresh()
                time.sleep(5)
                continue

    def enter_income_page(self):
        time.sleep(2)
        doc = "进入Income/Expense菜单"
        for i in range(3):
            try:
                self.wait_elevisble(AdminTicketManagementLocator.dashboard_tab, doc)
                self.click_element(AdminTicketManagementLocator.dashboard_tab, doc)
                time.sleep(1)
                self.wait_elevisble(AdminBillAdjustmentLocator.income_expense_tab,doc)
                self.click_element(AdminBillAdjustmentLocator.income_expense_tab,doc)
                break
            except:
                self.driver.refresh()
                time.sleep(5)
                continue

    def enter_user_income_page(self):
        time.sleep(2)
        doc = "进入User端 Income/Expense菜单"
        for i in range(3):
            try:
                self.wait_elevisble(AdminBillAdjustmentLocator.user_income_expense_tab,doc)
                self.click_element(AdminBillAdjustmentLocator.user_income_expense_tab,doc)
                break
            except:
                self.driver.refresh()
                time.sleep(5)
                continue

    #获取金额
    def get_amount(self,locate):
        doc = "获取金额"
        self.wait_elevisble(locate, doc)
        amount = self.get_element(locate, doc).text
        amount = amount.split(" ")
        amount = amount[1]
        return amount

    #调账成功
    def adjust_balance(self,email,amount):
        self.enter_adjustment_page()
        time.sleep(2)
        doc = "管理员给用户调账"
        # logging.info('>>>>>>>>>>>>test>>>>>>>>')
        self.wait_elevisble(AdminBillAdjustmentLocator.account_email,doc)
        self.send_keys(AdminBillAdjustmentLocator.account_email,email,doc)
        time.sleep(2)
        self.wait_elevisble(AdminBillAdjustmentLocator.adjust_amount,doc)
        self.send_keys(AdminBillAdjustmentLocator.adjust_amount,amount,doc)
        self.wait_elevisble(AdminBillAdjustmentLocator.remark,doc)
        self.send_keys(AdminBillAdjustmentLocator.remark,"remark",doc)
        self.wait_elevisble(AdminBillAdjustmentLocator.submit_button,doc)
        self.click_element(AdminBillAdjustmentLocator.submit_button,doc)
        time.sleep(2)
        before_amount = self.get_amount(AdminBillAdjustmentLocator.balance_amount_before_adjust)
        amount_just = self.get_amount(AdminBillAdjustmentLocator.adjust_amount_result)
        after_amount = self.get_amount(AdminBillAdjustmentLocator.balance_amount_after_adjust)
        before_amount = before_amount.replace(',','')
        after_amount = after_amount.replace(',', '')
        assert int(before_amount) + int(amount_just) == int(after_amount)
        assert int(amount_just) == amount
        self.wait_elevisble(AdminBillAdjustmentLocator.back_button,doc)
        self.click_element(AdminBillAdjustmentLocator.back_button,doc)
        time.sleep(2)
        try:
            self.wait_elevisble(AdminBillAdjustmentLocator.account_email, doc)
        except:
            # logging.info('>>>>>>>>>>>>调账结果页，back按钮失效')
            print('>>>>>>>>>>>>调账结果页，back按钮失效')

    # 调账失败
    def adjust_balance_fail(self, email, amount):
        self.enter_adjustment_page()
        time.sleep(2)
        doc = "管理员给用户调账失败"
        self.wait_elevisble(AdminBillAdjustmentLocator.account_email, doc)
        self.send_keys(AdminBillAdjustmentLocator.account_email, email, doc)
        time.sleep(2)
        self.wait_elevisble(AdminBillAdjustmentLocator.adjust_amount, doc)
        self.send_keys(AdminBillAdjustmentLocator.adjust_amount, amount, doc)
        self.wait_elevisble(AdminBillAdjustmentLocator.remark, doc)
        self.send_keys(AdminBillAdjustmentLocator.remark, "remark", doc)
        self.wait_elevisble(AdminBillAdjustmentLocator.submit_button, doc)
        self.click_element(AdminBillAdjustmentLocator.submit_button, doc)
        time.sleep(2)
        self.wait_elevisble(AdminBillAdjustmentLocator.adjust_fail,doc)
        text = self.get_element(AdminBillAdjustmentLocator.adjust_fail,doc).text
        assert "Adjust Failed" == text

    #查看Income/Expense
    def check_income_expense(self,email,type,amount):
        doc = "查看Income/Expense"
        self.wait_elevisble(AdminBillAdjustmentLocator.income_expense_tab,doc)
        self.click_element(AdminBillAdjustmentLocator.income_expense_tab,doc)
        time.sleep(5)
        self.wait_elevisble(AdminBillAdjustmentLocator.search_email,doc)
        self.send_keys(AdminBillAdjustmentLocator.search_email,email,doc)
        time.sleep(5)
        self.wait_elevisble(AdminBillAdjustmentLocator.get_email_value_locator(self,email),doc)
        self.click_element(AdminBillAdjustmentLocator.get_email_value_locator(self,email),doc)
        time.sleep(10)
        self.wait_elevisble(AdminBillAdjustmentLocator.search_transation_type,doc)
        self.click_element(AdminBillAdjustmentLocator.search_transation_type,doc)
        self.wait_elevisble(AdminBillAdjustmentLocator.get_transation_value_locator(self, type), doc)
        self.click_element(AdminBillAdjustmentLocator.get_transation_value_locator(self, type), doc)
        time.sleep(5)
        self.wait_elevisble(AdminBillAdjustmentLocator.last_income_record_anount,doc)
        search_amount = self.get_element(AdminBillAdjustmentLocator.last_income_record_anount,doc).text
        assert search_amount == amount + ".00"
        self.wait_elevisble(AdminBillAdjustmentLocator.last_income_record_no,doc)
        self.click_element(AdminBillAdjustmentLocator.last_income_record_no,doc)
        time.sleep(2)
        self.wait_elevisble(AdminBillAdjustmentLocator.detail_amount,doc)
        detail_amount = self.get_element(AdminBillAdjustmentLocator.detail_amount,doc).text
        assert detail_amount == amount + ".00"


    def search_income_expense(self,email,type):
        self.enter_income_page()
        doc = "搜索Income/Expense"
        self.wait_elevisble(AdminBillAdjustmentLocator.income_expense_tab, doc)
        self.click_element(AdminBillAdjustmentLocator.income_expense_tab, doc)
        time.sleep(5)
        self.wait_elevisble(AdminBillAdjustmentLocator.search_email, doc)
        self.send_keys(AdminBillAdjustmentLocator.search_email, email, doc)
        time.sleep(5)
        self.wait_elevisble(AdminBillAdjustmentLocator.get_email_value_locator(self, email), doc)
        self.click_element(AdminBillAdjustmentLocator.get_email_value_locator(self, email), doc)
        time.sleep(10)
        self.wait_elevisble(AdminBillAdjustmentLocator.search_transation_type, doc)
        self.click_element(AdminBillAdjustmentLocator.search_transation_type, doc)
        self.wait_elevisble(AdminBillAdjustmentLocator.get_transation_value_locator(self, type), doc)
        self.click_element(AdminBillAdjustmentLocator.get_transation_value_locator(self, type), doc)
        time.sleep(5)
        self.wait_elevisble(AdminBillAdjustmentLocator.search_time,doc)
        self.click_element(AdminBillAdjustmentLocator.search_time,doc)
        self.wait_elevisble(AdminBillAdjustmentLocator.pre_button,doc)
        self.click_element(AdminBillAdjustmentLocator.pre_button,doc)
        time.sleep(2)
        self.wait_elevisble(AdminBillAdjustmentLocator.date1,doc)
        self.click_element(AdminBillAdjustmentLocator.date1,doc)
        self.wait_elevisble(AdminBillAdjustmentLocator.next_button,doc)
        self.click_element(AdminBillAdjustmentLocator.next_button,doc)
        self.wait_elevisble(AdminBillAdjustmentLocator.date2,doc)
        self.click_element(AdminBillAdjustmentLocator.date2,doc)
        time.sleep(5)
        self.wait_elevisble(AdminBillAdjustmentLocator.last_income_record_type,doc)
        search_result = self.get_element(AdminBillAdjustmentLocator.last_income_record_type,doc).text
        assert search_result == type
        self.wait_elevisble(AdminBillAdjustmentLocator.last_income_record_account,doc)
        search_result = self.get_element(AdminBillAdjustmentLocator.last_income_record_account,doc).text
        assert search_result == email


    def user_search_income_expense(self,email,type):
        self.enter_user_income_page()
        doc = "User端 搜索Income/Expense"
        time.sleep(2)
        self.wait_elevisble(AdminBillAdjustmentLocator.user_search_transation_type, doc)
        self.click_element(AdminBillAdjustmentLocator.user_search_transation_type, doc)
        self.wait_elevisble(AdminBillAdjustmentLocator.get_transation_value_locator(self, type), doc)
        self.click_element(AdminBillAdjustmentLocator.get_transation_value_locator(self, type), doc)
        time.sleep(5)
        self.wait_elevisble(AdminBillAdjustmentLocator.search_time,doc)
        self.click_element(AdminBillAdjustmentLocator.search_time,doc)
        self.wait_elevisble(AdminBillAdjustmentLocator.pre_button,doc)
        self.click_element(AdminBillAdjustmentLocator.pre_button,doc)
        time.sleep(2)
        self.wait_elevisble(AdminBillAdjustmentLocator.date1,doc)
        self.click_element(AdminBillAdjustmentLocator.date1,doc)
        self.wait_elevisble(AdminBillAdjustmentLocator.next_button,doc)
        self.click_element(AdminBillAdjustmentLocator.next_button,doc)
        self.wait_elevisble(AdminBillAdjustmentLocator.date2,doc)
        self.click_element(AdminBillAdjustmentLocator.date2,doc)
        time.sleep(5)
        self.wait_elevisble(AdminBillAdjustmentLocator.last_income_record_type,doc)
        search_result = self.get_element(AdminBillAdjustmentLocator.last_income_record_type,doc).text
        assert search_result == type
        self.wait_elevisble(AdminBillAdjustmentLocator.last_income_record_account,doc)
        search_result = self.get_element(AdminBillAdjustmentLocator.last_income_record_account,doc).text
        assert search_result == email


    def user_filter_incmoe_expense(self):
        self.enter_user_income_page()
        doc = "按照income、expense筛选搜索"
        self.wait_elevisble(AdminBillAdjustmentLocator.income_expense_filter,doc)
        self.click_element(AdminBillAdjustmentLocator.income_expense_filter,doc)
        self.wait_elevisble(AdminBillAdjustmentLocator.income_item,doc)
        self.click_element(AdminBillAdjustmentLocator.income_item,doc)
        self.wait_elevisble(AdminBillAdjustmentLocator.income_expense_filter_ok_button,doc)
        self.click_element(AdminBillAdjustmentLocator.income_expense_filter_ok_button,doc)
        time.sleep(5)
        self.wait_elevisble(AdminBillAdjustmentLocator.last_income_record_income_expense,doc)
        temp = self.get_element(AdminBillAdjustmentLocator.last_income_record_income_expense,doc).text
        assert temp == "Income"


    def user_view_income_detail(self):
        self.enter_user_income_page()
        doc = "查看income、expense的记录详情页"
        time.sleep(5)
        self.wait_elevisble(AdminBillAdjustmentLocator.last_income_record_no,doc)
        list_tran_no = self.get_element(AdminBillAdjustmentLocator.last_income_record_no,doc).text
        list_account = self.get_element(AdminBillAdjustmentLocator.last_income_record_account,doc).text
        list_timestamp = self.get_element(AdminBillAdjustmentLocator.last_income_record_timestamp,doc).text
        list_tran_type = self.get_element(AdminBillAdjustmentLocator.last_income_record_type,doc).text
        list_amount = self.get_element(AdminBillAdjustmentLocator.last_income_record_amount_user,doc).text
        list_tran_channel = self.get_element(AdminBillAdjustmentLocator.last_income_record_tran_channel,doc).text
        list_income = self.get_element(AdminBillAdjustmentLocator.last_income_record_income_expense,doc).text
        list_journal_number = self.get_element(AdminBillAdjustmentLocator.last_income_record_journal_number,doc).text
        self.click_element(AdminBillAdjustmentLocator.last_income_record_no,doc)
        time.sleep(1)
        self.wait_elevisble(AdminBillAdjustmentLocator.detail_transaction_no,doc)
        assert list_tran_no == self.get_element(AdminBillAdjustmentLocator.detail_transaction_no,doc).text
        assert list_account == self.get_element(AdminBillAdjustmentLocator.detail_account_email, doc).text
        assert list_timestamp == self.get_element(AdminBillAdjustmentLocator.detail_timestamp, doc).text
        assert list_tran_type == self.get_element(AdminBillAdjustmentLocator.detail_tran_type, doc).text
        assert list_amount == self.get_element(AdminBillAdjustmentLocator.detail_amount, doc).text
        assert list_tran_channel == self.get_element(AdminBillAdjustmentLocator.detail_tran_channel, doc).text
        assert list_income == self.get_element(AdminBillAdjustmentLocator.detail_income_expense, doc).text
        assert list_journal_number == self.get_element(AdminBillAdjustmentLocator.detail_journal_number, doc).text

    # 根据tab菜单名称进行点击
    def click_my_amount_adjustment_list(self, tab_name):
        doc = '根据tab菜单名字进行点击进入'
        time.sleep(5)
        self.wait_elevisble(AdminBillAdjustmentLocator.my_amount_adjustment_list, doc)
        my_resource_tab_list = self.get_elements(AdminBillAdjustmentLocator.my_amount_adjustment_list, doc)
        for i in my_resource_tab_list:
            print('*****:', i.text, type(i))
            if i.text == tab_name:
                print('++++:', i.text)
                i.click()
        time.sleep(2)

    #进入batch operation页面
    def enter_batch_operation(self):
        doc = '进入batch operation页面'
        self.click_my_amount_adjustment_list('Batch Operation')
        time.sleep(5)
        print("if_please_download:", self.get_element(AdminBillAdjustmentLocator.if_please_download, doc).text)
        print('if_operation_id:', self.get_element(AdminBillAdjustmentLocator.if_operation_id, doc).get_attribute('placeholder'))
        print('search:', self.get_element(AdminBillAdjustmentLocator.if_search, doc).text)
        print('if_operation_list:', self.get_element(AdminBillAdjustmentLocator.if_operation_list, doc).text)
        assert self.get_element(AdminBillAdjustmentLocator.if_please_download, doc).text == 'Please download the template for amount adjustment first, and then upload the Excel after filling in.Download Template'
        assert self.get_element(AdminBillAdjustmentLocator.if_operation_id, doc).get_attribute('placeholder') == 'Please enter operation ID'
        assert self.get_element(AdminBillAdjustmentLocator.if_search, doc).text == 'Search'
        assert self.get_element(AdminBillAdjustmentLocator.if_operation_list, doc).text == 'Operation ID'

    #进入amount adjustment页面
    def check_amount_adjustment_ui(self):
        doc = '进入amount adjustment页面'
        time.sleep(1)
        self.check_attribute(self.get_element(AdminBillAdjustmentLocator.adjustment_tab, doc).text, 'Amount Adjustment')
        self.check_attribute(self.get_element(AdminBillAdjustmentLocator.amount_adjustment_title, doc).text, 'Amount Adjustment')
        # 点击进入Single Operation页签
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.single_operation_page, doc)
        time.sleep(1)
        # 点击进入Batch Operation页签
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_page, doc)
        time.sleep(1)

################################################################################
    # 获取第一个sheet名称
    def excel_sheet_name(self, locate):
        doc = '判断sheet名字'
        excel_path = xlrd.open_workbook(locate)
        # 获取第一个sheet
        sheet = excel_path.sheet_by_index(0).name
        print("文件sheet页面名称：", sheet)
        return sheet

    # 判断文件存在某字段
    def excel_element_is_exist(self, locate, row, list_field_value):
        doc = '判断表格字段是否存在'
        excel_path = xlrd.open_workbook(locate)
        excel_sheet = excel_path.sheet_by_index(0)
        # 读取文件
        list_fileds = excel_sheet.row_values(row)
        count = 0
        for list_filed in list_fileds:
            if list_filed != list_field_value:
                count += 1
            else:
                assert True
                print("文件包含字段：", list_field_value)

    # 文件写入数据
    def excel_write_data(self, locate, sheet_name, x, value):
        doc = '文件写入数据'
        workbook = openpyxl.load_workbook(locate)
        # 通过名称获取工作薄
        worksheet = workbook[sheet_name]
        # 在x中写入value
        worksheet[x] = value
        # 保存
        workbook.save(locate)

    # # 文件写入数据
    # def excel_write_data_xlrd(self, locate, sheet_name, x, value):
    #     doc = '文件写入数据'
    #     file = xlrd.open_workbook(locate)
    #     # 通过名称获取工作薄
    #     worksheet = file.sheet_namas()[0]
    #     # 在x中写入value
    #     worksheet[x] = value
    #     # 保存
    #     workbook.save(locate)

    def is_sorted(self, check):
        asc = desc = True
        i, L = 0, len(check)
        while (i < (L - 1)) and (asc or desc):
            if check[i] < check[i + 1]:
                desc = False
            elif check[i] > check[i + 1]:
                asc = False
            if not (asc or desc):
                break
            i += 1
        if asc and desc:
            return "升序 & 降序"
        elif asc:
            return "升序"
        elif desc:
            return "降序"
        else:
            return "不是升序也不是降序"

    # 获取字符串中的数字
    def get_nums_from_string(self, values):
        nums = nums_from_string.get_nums(values)
        # 返回列表，转化为字符串
        string_num = ''.join(str(e) for e in nums)
        return string_num

    # 获取页面的页码信息,记录总数、分页大小
    def get_page_size(self, totalitems, pagesizes):
        doc = '页码'
        totalitem = self.get_nums_from_string(totalitems)
        pagesize = self.get_nums_from_string(pagesizes)
        totalpage = math.ceil(int(totalitem) / int(pagesize))
        return totalpage

    # 翻页处理，前一页，后一页
    def operate_page(self, totalpage, nextpage):
        doc = '翻页'
        if totalpage == 1:
            print("当前只有一页，无需翻页处理")
        else:
            print("当前共有{0}页，可以翻页处理".format(totalpage))
            for i in range(1, totalpage):
                self.move_mouse_to_click_element(nextpage, doc)
                time.sleep(1)
                print("开始翻页，第{0}次翻页".format(i))

    # 上传文件
    def upload_file(self, locate, file_path):
        doc = '上传文件'
        time.sleep(1)
        # 定位input元素
        upload_file = self.get_element(locate, doc)
        # 上传文件
        upload_file.send_keys(file_path)
        time.sleep(3)

    # 更改文件名
    def update_file_name(self, path, filename, newfilename):
        # 设置旧文件名（就是路径+文件名）
        oldname = path + os.sep + filename  # os.sep添加系统分隔符
        # 设置新文件名
        newname = path + os.sep + newfilename
        os.rename(oldname, newname)  # 用os模块中的rename方法对文件改名
        print(oldname, '======>', newname)
        return newname

    # 新建文件
    def create_file(self, path, name, file_type, msg):
        doc = '新建文件'
        time.sleep(1)
        full_file_path = f'{path}/{name + file_type}'
        print(full_file_path)
        file = open(full_file_path, 'w')    # w的含义为可进行读写
        file.write(msg)        # file.write()为写入指令
        file.close()
        return full_file_path

    # 移除文件
    def remove_file(self, excel_path_vlalue):
        os.remove(excel_path_vlalue)

    ################################################################################

    #进入Batch Operation页面下载模板
    def batch_operation_download_template(self):
        doc = '进入batch operation页面'
        time.sleep(1)
       # 点击进入Batch Operation页签
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_page, doc)
        time.sleep(1)
        # 点击模板下载按钮
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_download_button, doc)
        time.sleep(3)
        download_template_file_name = 'Amount_Adjustment_Template'
        # path = 'C:/Users/25294/Downloads'
        path = '/home/seluser/Downloads'
        files = os.listdir(path)
        for file in files:
            if file[0:26] == download_template_file_name:
                excel_path_vlalue = (f'{path}/{file}')
                print(excel_path_vlalue)
                self.excel_element_is_exist(excel_path_vlalue, 0, '*Account Email')
                self.excel_element_is_exist(excel_path_vlalue, 0, '*Adjust Amount')
                self.excel_element_is_exist(excel_path_vlalue, 0, '*Currency')
                self.excel_element_is_exist(excel_path_vlalue, 0, '*Send Notification')
                self.excel_element_is_exist(excel_path_vlalue, 0, '*Remark')
                self.check_attribute(self.excel_sheet_name(excel_path_vlalue),'Amount Adjustment')
                # self.excel_write_data(excel_path_vlalue,'Amount Adjustment', 'B2', '-1000.00')
                os.remove(excel_path_vlalue)
                break
            else:
                assert False,"文件不存在"

    # #进入Batch Operation页面下载模板
    # def batch_adjustment_hidden(self, cmdopt, get_data_source):
    #     doc = '进入batch operation页面'
    #     time.sleep(1)
    #     id = BusinessCommonTools().update_batch_amount_adjustment_true(cmdopt, get_data_source)
    #     print(id)

    # # 进入Batch Operation列表
    # def batch_operation_list_sort(self):
    #     doc = '进入Batch operation页面'
    #     time.sleep(1)
    #    # 点击进入Batch Operation页签
    #     self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_page, doc)
    #     time.sleep(1)
    #     operation_ids = self.get_elements(AdminBillAdjustmentLocator.batch_operation_id, doc).get_attribute("textContent")
    #     print(operation_ids)
    #     sotr_result = self.is_sorted(operation_ids)
    #     if sotr_result == '降序':
    #         assert True, '数据是降序排序'
    #     else:
    #         assert False, '数据不是降序排序'

    # 进入Batch Operation列表刷新
    def batch_operation_list_refresh(self):
        doc = '进入Batch operation页面'
        time.sleep(1)
        # 点击进入Batch Operation页签
        self.wait_elevisble(AdminBillAdjustmentLocator.batch_operation_page, doc)
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_page, doc)
        time.sleep(1)
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_dropdown_box, doc)
        self.wait_elevisble(AdminBillAdjustmentLocator.batch_operation_operator, doc)
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_operator, doc)
        self.check_attribute(self.get_element(AdminBillAdjustmentLocator.batch_operation_value, doc).text, 'Operator')
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_reset, doc)
        self.check_attribute(self.get_element(AdminBillAdjustmentLocator.batch_operation_value, doc).text, 'Operation ID')

    #进入Batch Operation列表页面切换
    def batch_operation_list_page_switching(self):
        doc = '进入Batch operation页面'
        time.sleep(1)
       # 点击进入Batch Operation页签
        self.wait_elevisble(AdminBillAdjustmentLocator.batch_operation_page, doc)
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_page, doc)
        time.sleep(1)
        totalitem = self.get_element(AdminBillAdjustmentLocator.batch_operation_total_item, doc).text
        pagesize = self.get_element(AdminBillAdjustmentLocator.batch_operation_page_switching_button_value, doc).text
        totalpage = self.get_page_size(totalitem, pagesize)
        # 翻页
        self.operate_page(totalpage, AdminBillAdjustmentLocator.batch_operation_next_page)
        time.sleep(1)
        self.wait_elevisble(AdminBillAdjustmentLocator.batch_operation_id_0000001, doc)
        self.check_attribute(self.get_element(AdminBillAdjustmentLocator.batch_operation_id_0000001, doc).text,'0000001')
        # 数量设置为15
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_page_switching_button, doc)
        self.wait_elevisble(AdminBillAdjustmentLocator.batch_operation_page_switching_button_15, doc)
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_page_switching_button_15, doc)
        time.sleep(1)
        self.check_attribute(self.get_element(AdminBillAdjustmentLocator.batch_operation_page_switching_button_value, doc).text,'15 / page')
        pagesize = self.get_element(AdminBillAdjustmentLocator.batch_operation_page_switching_button_value, doc).text
        totalpage = self.get_page_size(totalitem, pagesize)
        # 翻页
        self.operate_page(totalpage, AdminBillAdjustmentLocator.batch_operation_next_page)
        time.sleep(1)
        self.wait_elevisble(AdminBillAdjustmentLocator.batch_operation_id_0000001, doc)
        self.check_attribute(self.get_element(AdminBillAdjustmentLocator.batch_operation_id_0000001, doc).text, '0000001')

    # 进入Batch Operation搜索调账记录
    def batch_operation_search_record(self):
        doc = '进入Batch operation页面'
        time.sleep(1)
        # 点击进入Batch Operation页签
        self.wait_elevisble(AdminBillAdjustmentLocator.batch_operation_page, doc)
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_page, doc)
        time.sleep(1)
        self.send_keys(AdminBillAdjustmentLocator.operation_id_input_field, '0000001', doc)
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_search, doc)
        time.sleep(1)
        self.check_attribute(self.get_element(AdminBillAdjustmentLocator.batch_operation_id_0000001, doc).text, '0000001')
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.operation_id_input_field, doc)
        self.element_clear(AdminBillAdjustmentLocator.operation_id_input_field, doc)
        time.sleep(1)
        self.send_keys(AdminBillAdjustmentLocator.operation_id_input_field, '1', doc)
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_search, doc)
        time.sleep(1)
        self.check_attribute(self.get_element(AdminBillAdjustmentLocator.batch_operation_no_data, doc).text, 'No Data')
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_dropdown_box, doc)
        self.wait_elevisble(AdminBillAdjustmentLocator.batch_operation_operator, doc)
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_operator, doc)
        self.send_keys(AdminBillAdjustmentLocator.operation_id_input_field, 'lwm528675013', doc)
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_search, doc)
        time.sleep(1)
        self.check_attribute(self.get_element(AdminBillAdjustmentLocator.batch_operation_operator_data, doc).text, 'lwm528675013@163.com')

    # # 打开窗口上传文件
    # def open_window_to_upload_file(self, path, file):
    #     doc = '翻页'
    #     # 使用pywinauto来选择文件
    #     app = pywinauto.Desktop()
    #     # 选择文件上传的窗口
    #     dlg = app["打开"]
    #     # 选择文件地址输入框，点击激活
    #     dlg["Toolbar3"].click()
    #     # 键盘输入上传文件的路径
    #     self.send_keys(path, doc)
    #     # 键盘输入回车，打开该路径
    #     self.send_keys("{VK_RETURN}", doc)
    #     # 选中文件名输入框，输入文件名
    #     dlg["文件名(&N):Edit"].type_keys(file)
    #     # 点击打开
    #     dlg["打开(&O)"].click()

    # 上传调账文件
    def batch_operation_upload_file(self):
        doc = '进入Batch operation页面'
        time.sleep(1)
        # 点击进入Batch Operation页签
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_page, doc)
        time.sleep(1)
        # 点击模板下载按钮
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_download_button, doc)
        time.sleep(1)
        download_template_file_name = 'Amount_Adjustment_Template.'
        # path = 'C:/Users/25294/Downloads'
        path = '/home/seluser/Downloads'
        files = os.listdir(path)
        for file in files:
            if file[0:27] == download_template_file_name:
                excel_path_vlalue = f'{path}/{file}'
                self.excel_write_data(excel_path_vlalue,'Amount Adjustment', 'A2', 'Etest1@sigma.com')
                # self.excel_write_data(excel_path_vlalue,'Amount Adjustment', 'A2', 'testqing1@qq.com')
                self.excel_write_data(excel_path_vlalue,'Amount Adjustment', 'B2', '2.00')
                self.excel_write_data(excel_path_vlalue,'Amount Adjustment', 'C2', 'IDR')
                self.excel_write_data(excel_path_vlalue,'Amount Adjustment', 'D2', 'No')
                self.excel_write_data(excel_path_vlalue,'Amount Adjustment', 'E2', '11')
                self.upload_file(AdminBillAdjustmentLocator.operation_id_upload_file, excel_path_vlalue)
                break
            else:
                assert False, "文件不存在"
        # 判断文件上传是否成功
        self.wait_elevisble(AdminBillAdjustmentLocator.operation_id_upload_file_tips, doc)
        self.check_attribute(self.get_element(AdminBillAdjustmentLocator.operation_id_upload_file_tips, doc).text, 'Uploaded successfully.')
        operation_id_new = self.get_element(AdminBillAdjustmentLocator.batch_operation_operation_id_new, doc).text
        tips_value = 'The backend is processing data, you can search for the result on the current page according to your operation ID: ' + operation_id_new + '.'
        self.check_attribute(self.get_element(AdminBillAdjustmentLocator.operation_id_upload_file_tips_value, doc).text, tips_value)
        self.check_attribute(self.get_element(AdminBillAdjustmentLocator.batch_operation_operator_new, doc).text, 'lwm528675013@163.com')
        now_time = datetime.datetime.now().strftime("%d-%m-%Y")
        operation_time = self.get_element(AdminBillAdjustmentLocator.batch_operation_operation_time_new, doc).text
        self.check_attribute(operation_time[0:10], now_time)
        # self.check_attribute(self.get_element(AdminBillAdjustmentLocator.batch_operation_operation_status_new, doc).text, 'Successful')
        self.check_attribute(self.get_element(AdminBillAdjustmentLocator.batch_operation_actions_new, doc).text, 'Result File')

    # 上传调账文件格式校验
    def batch_operation_file_format_check(self):
        doc = '进入Batch operation页面'
        time.sleep(1)
        # 点击进入Batch Operation页签
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_page, doc)
        time.sleep(1)
        # path = 'C:/Users/25294/Downloads'
        path = '/home/seluser/Downloads'
        test_name = 'test' + datetime.datetime.now().strftime("%Y%m%d%H%M%S")
        fiel_path_txt = self.create_file(path, test_name, '.txt', 'hello')
        fiel_path_doc = self.create_file(path, test_name, '.doc', 'hello')
        self.upload_file(AdminBillAdjustmentLocator.operation_id_upload_file, fiel_path_txt)
        self.remove_file(fiel_path_txt)
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_upload_fail_prompt_box, doc)
        self.check_attribute(self.get_element(AdminBillAdjustmentLocator.batch_operation_upload_fail_prompt_box, doc).text, 'Invalid excel file type ".txt", should be ".xlsx, .xls, .csv".')
        time.sleep(3)
        self.upload_file(AdminBillAdjustmentLocator.operation_id_upload_file, fiel_path_doc)
        self.remove_file(fiel_path_doc)
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_upload_fail_prompt_box, doc)
        self.check_attribute(self.get_element(AdminBillAdjustmentLocator.batch_operation_upload_fail_prompt_box, doc).text, 'Invalid excel file type ".doc", should be ".xlsx, .xls, .csv".')
        time.sleep(3)
        file_new_xls = self.update_file_name(path, 'Amount_Adjustment_Template.xlsx', 'Amount_Adjustment_Template.xls')
        self.upload_file(AdminBillAdjustmentLocator.operation_id_upload_file, file_new_xls)
        self.wait_elevisble(AdminBillAdjustmentLocator.operation_id_upload_file_tips, doc)
        self.check_attribute(self.get_element(AdminBillAdjustmentLocator.operation_id_upload_file_tips, doc).text, 'Uploaded successfully.')
        time.sleep(3)
        file_new_csv = self.update_file_name(path, 'Amount_Adjustment_Template.xls', 'Amount_Adjustment_Template.csv')
        self.upload_file(AdminBillAdjustmentLocator.operation_id_upload_file, file_new_csv)
        self.wait_elevisble(AdminBillAdjustmentLocator.operation_id_upload_file_tips, doc)
        self.check_attribute(self.get_element(AdminBillAdjustmentLocator.operation_id_upload_file_tips, doc).text, 'Uploaded successfully.')
        self.remove_file(file_new_csv)

    def enter_dashboard_pages(self):
        time.sleep(2)
        doc = "进入Dashboard界面"
        for i in range(3):
            try:
                eles = self.get_elements(AdminBillAdjustmentLocator.default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(AdminBillAdjustmentLocator.dashboard_title_btn, doc)
                self.click_element(AdminBillAdjustmentLocator.dashboard_title_btn, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(AdminBillAdjustmentLocator.dashboard_page, doc)

    # 获取客户的现金账本
    def get_client_cash(self):
        doc = '进入客户Dashboard页面'
        time.sleep(1)
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.dashboard_page, doc)
        self.wait_elevisble(AdminBillAdjustmentLocator.available_balance_value, doc)
        input_amount = self.get_element(AdminBillAdjustmentLocator.available_balance_value, doc).text
        available_balance = float(input_amount.replace(',', ''))
        time.sleep(1)
        return available_balance

    # 执行一个客户的调账
    def perform_customer_reconciliation(self):
        doc = '进入Batch operation页面'
        time.sleep(1)
        # 点击进入Batch Operation页签
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_page, doc)
        time.sleep(1)
        # path = 'C:/Users/25294/Downloads'
        path = '/home/seluser/Downloads'
        # 上传文件
        self.batch_operation_upload_file()
        self.remove_file(f'{path}/Amount_Adjustment_Template.xlsx')
        # 下载处理结果
        self.wait_elevisble(AdminBillAdjustmentLocator.upload_success_confirmation_box, doc)
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.upload_success_confirmation_box, doc)
        time.sleep(3)
        # 刷新页面
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_reset, doc)
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.processing_result_template, doc)
        time.sleep(3)
        files = os.listdir(path)
        download_template_file_name = 'Amount_Adjustment_Template_'
        for file in files:
            if file[0:27] == download_template_file_name:
                excel_path_vlalue = (f'{path}/{file}')
                print(excel_path_vlalue)
                self.excel_element_is_exist(excel_path_vlalue, 0, 'Transaction ID')
                self.excel_element_is_exist(excel_path_vlalue, 0, 'Balance Amount Before Adjustment (IDR)')
                self.excel_element_is_exist(excel_path_vlalue, 0, 'Balance Amount After Adjustment (IDR)')
                os.remove(excel_path_vlalue)
                break
            else:
                assert False,"文件不存在"

    # 文件中存在异常账号，无法调账成功
    def perform_abnormal_customer_reconciliation(self):
        doc = '进入Batch operation页面'
        time.sleep(1)
        # 点击进入Batch Operation页签
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_page, doc)
        time.sleep(1)
        # 点击模板下载按钮
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_download_button, doc)
        time.sleep(1)
        download_template_file_name = 'Amount_Adjustment_Template.'
        # path = 'C:/Users/25294/Downloads'
        path = '/home/seluser/Downloads'
        # self.top_up(path, 'lwm528675013@163.com', '2.00', 'IDR', 'No', 'admin mail')
        files = os.listdir(path)
        for file in files:
            if file[0:27] == download_template_file_name:
                excel_path_vlalue = f'{path}/{file}'
                self.excel_write_data(excel_path_vlalue, 'Amount Adjustment', 'A2', 'lwm528675013@163.com')
                self.excel_write_data(excel_path_vlalue, 'Amount Adjustment', 'B2', '2.00')
                self.excel_write_data(excel_path_vlalue, 'Amount Adjustment', 'C2', 'IDR')
                self.excel_write_data(excel_path_vlalue, 'Amount Adjustment', 'D2', 'No')
                self.excel_write_data(excel_path_vlalue, 'Amount Adjustment', 'E2', '11')
                self.excel_write_data(excel_path_vlalue, 'Amount Adjustment', 'A3', 'testabnormal@qq.com')
                self.excel_write_data(excel_path_vlalue, 'Amount Adjustment', 'B3', '2.00')
                self.excel_write_data(excel_path_vlalue, 'Amount Adjustment', 'C3', 'IDR')
                self.excel_write_data(excel_path_vlalue, 'Amount Adjustment', 'D3', 'No')
                self.excel_write_data(excel_path_vlalue, 'Amount Adjustment', 'E3', '11')
                self.upload_file(AdminBillAdjustmentLocator.operation_id_upload_file, excel_path_vlalue)
                os.remove(excel_path_vlalue)
                break
            else:
                assert False, "文件不存在"
        # 判断文件上传是否成功
        self.wait_elevisble(AdminBillAdjustmentLocator.operation_id_upload_file_tips, doc)
        self.check_attribute(self.get_element(AdminBillAdjustmentLocator.operation_id_upload_file_tips, doc).text,'Uploaded successfully.')
        self.wait_elevisble(AdminBillAdjustmentLocator.upload_success_confirmation_box, doc)
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.upload_success_confirmation_box, doc)
        time.sleep(3)
        # 刷新页面
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_reset, doc)
        self.check_attribute(self.get_element(AdminBillAdjustmentLocator.batch_operation_operation_status_new, doc).text, 'Failed')
        self.move_mouse_to_click_element(AdminBillAdjustmentLocator.processing_result_template, doc)
        time.sleep(3)
        # self.check_uploaded()
        # self.top_up_resul( path, 0, 'Failed Note', 1, 'Account email is invalid.')
        files = os.listdir(path)
        download_template_file_name = 'Amount_Adjustment_Template_'
        for file in files:
            if file[0:27] == download_template_file_name:
                excel_path_vlalue = (f'{path}/{file}')
                print(excel_path_vlalue)
                self.excel_element_is_exist(excel_path_vlalue, 0, 'Failed Note')
                self.excel_element_is_exist(excel_path_vlalue, 1, 'Account email is invalid.')
                self.excel_element_is_exist(excel_path_vlalue, 2, 'Account email is invalid.')
                os.remove(excel_path_vlalue)
                break
            else:
                assert False,"文件不存在"

    # #
    # # 文件中存在异常账号，无法调账成功
    # def perform_required_fields_reconciliation(self):
    #     doc = '进入Batch operation页面'
    #     time.sleep(1)
    #     # 点击进入Batch Operation页签
    #     self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_page, doc)
    #     time.sleep(1)
    #     # 点击模板下载按钮
    #     self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_download_button, doc)
    #     time.sleep(1)
    #     # download_template_file_name = 'Amount_Adjustment_Template.'
    #     path = 'C:/Users/25294/Downloads'
    #     # # path = '/home/seluser/Downloads'
    #     self.top_up(path, '', '2.00', 'IDR', 'No', 'Required fields')
    #     self.check_uploaded()
    #     self.top_up_resul( path, 0, 'Failed Note', 1, 'Incomplete information.')
    #     # self.top_up(path, 'testqing1@qq.com', '', 'IDR', 'No', 'Required fields')
    # #     # self.check_uploaded()
    # #     # self.top_up_resul( path, 0, 'Failed Note', 1, 'Incomplete information.')
    # #     # self.top_up(path, 'testqing1@qq.com', '2.00', '', 'No', 'Required fields')
    # #     # self.check_uploaded()
    # #     # self.top_up_resul( path, 0, 'Failed Note', 1, 'Incomplete information.')
    # #     # self.top_up(path, 'testqing1@qq.com', '2.00', 'IDR', '', 'Required fields')
    # #     # self.check_uploaded()
    # #     # self.top_up_resul( path, 0, 'Failed Note', 1, 'Incomplete information.')
    # #     # self.top_up(path, 'testqing1@qq.com', '2.00', 'IDR', 'No', '')
    #
    #
    # def top_up(self,path, email, amount, currency, notification, remark):
    #     doc = '进入Batch operation页面'
    #     time.sleep(1)
    #     download_template_file_name = 'Amount_Adjustment_Template.'
    #     files = os.listdir(path)
    #     for file in files:
    #         if file[0:27] == download_template_file_name:
    #             excel_path_vlalue = f'{path}/{file}'
    #             self.excel_write_data(excel_path_vlalue, 'Amount Adjustment', 'A2', email)
    #             self.excel_write_data(excel_path_vlalue, 'Amount Adjustment', 'B2', amount)
    #             self.excel_write_data(excel_path_vlalue, 'Amount Adjustment', 'C2', currency)
    #             self.excel_write_data(excel_path_vlalue, 'Amount Adjustment', 'D2', notification)
    #             self.excel_write_data(excel_path_vlalue, 'Amount Adjustment', 'E2', remark)
    #             self.upload_file(AdminBillAdjustmentLocator.operation_id_upload_file, excel_path_vlalue)
    #             os.remove(excel_path_vlalue)
    #             break
    #         else:
    #             assert False, "文件不存在"
    #
    # def top_up_resul(self, path, row, value, row2, value2):
    #     doc = '进入Batch operation页面'
    #     time.sleep(1)
    #     files = os.listdir(path)
    #     download_template_file_name = 'Amount_Adjustment_Template_'
    #     for file in files:
    #         if file[0:27] == download_template_file_name:
    #             excel_path_vlalue = (f'{path}/{file}')
    #             print(excel_path_vlalue)
    #             self.excel_element_is_exist(excel_path_vlalue, row, value)
    #             self.excel_element_is_exist(excel_path_vlalue, row2, value2)
    #             os.remove(excel_path_vlalue)
    #             break
    #         else:
    #             assert False,"文件不存在"
    #
    # def check_uploaded(self):
    #     doc = '进入Batch operation页面'
    #     time.sleep(1)
    #     self.wait_elevisble(AdminBillAdjustmentLocator.operation_id_upload_file_tips, doc)
    #     self.check_attribute(self.get_element(AdminBillAdjustmentLocator.operation_id_upload_file_tips, doc).text,'Uploaded successfully.')
    #     self.wait_elevisble(AdminBillAdjustmentLocator.upload_success_confirmation_box, doc)
    #     self.move_mouse_to_click_element(AdminBillAdjustmentLocator.upload_success_confirmation_box, doc)
    #     time.sleep(3)
    #     # 刷新页面
    #     self.move_mouse_to_click_element(AdminBillAdjustmentLocator.batch_operation_reset, doc)
    #     self.check_attribute(self.get_element(AdminBillAdjustmentLocator.batch_operation_operation_status_new, doc).text, 'Failed')
    #     self.move_mouse_to_click_element(AdminBillAdjustmentLocator.processing_result_template, doc)
    #     time.sleep(3)